package com.qf.day03.practice;

import java.util.Scanner;

/**
 * 用户输入任意一个整数，求各位数字之和。
 */
public class Summation {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入一个整数：");
        int num = scanner.nextInt();

        // 将整数转换为字符串并逐个取得每个数字进行相加
        String strNum = Integer.toString(num);
        int sum = 0;
        for (int i = 0; i < strNum.length(); i++) {
            char digitChar = strNum.charAt(i);
            int digit = Character.getNumericValue(digitChar);
            sum += digit;
        }

        System.out.println("各位数字之和为：" + sum);
    }
}
